package web.PetStore.web.servlet;

import web.PetStore.domain.Account;
import web.PetStore.service.CartService;
import web.PetStore.service.OrderService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(name = "CartFormServlet", urlPatterns = {"/CartForm"})
public class CartFormServlet extends HttpServlet {
    private static final String CART_FORM = "WEB-INF/jsp/cart/Cart.jsp";
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        HttpSession session = req.getSession();
        OrderService orderService = new OrderService();
        Account user = (Account) session.getAttribute("loginUser");
        session.setAttribute("cartList", new CartService().getOrderByUserid(user.getUserid()));
        req.getRequestDispatcher(CART_FORM).forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
